home *** CD-ROM | disk | FTP | other *** search
/ cyber.net interactivo 1997 March / inter@ivo 1997-03.iso / wing / dumb3d.hp_ / dumb3d.hp
Text File  |  1994-08-13  |  10KB  |  433 lines

  1. /**************************************************************************
  2.   dumb3d.hpp - A simple linear algebra library for 3D.
  3.  
  4.  **************************************************************************/
  5. /**************************************************************************
  6.  
  7.     (C) Copyright 1994 Microsoft Corp.  All rights reserved.
  8.  
  9.     You have a royalty-free right to use, modify, reproduce and 
  10.     distribute the Sample Files (and/or any modified version) in 
  11.     any way you find useful, provided that you agree that 
  12.     Microsoft has no warranty obligations or liability for any 
  13.     Sample Application Files which are modified. 
  14.  
  15.  **************************************************************************/
  16.  
  17. #if !defined(DUMB3D_HPP)
  18. #define DUMB3D_HPP
  19.  
  20. /*----------------------------------------------------------------------------
  21.  
  22. This header contains the declarations for the dumb3d functions.
  23.  
  24. */
  25.  
  26.  
  27. // real type
  28.  
  29. typedef float real;
  30.  
  31.  
  32. #define M_PI            3.14159265358979323846
  33.  
  34.  
  35. // forward declarations
  36.  
  37. class point_4;
  38. class vector_4;
  39. class matrix_4x4;
  40.  
  41. /*----------------------------------------------------------------------------
  42.  
  43. globally useful functions.
  44.  
  45. */
  46.  
  47. inline vector_4 operator-( point_4 const &Operand1, point_4 const &Operand2 );
  48.  
  49. inline vector_4 operator+( vector_4 const &Operand1,
  50.     vector_4 const &Operand2 );
  51.  
  52. inline vector_4 operator-( vector_4 const &Operand1,
  53.     vector_4 const &Operand2 );
  54.  
  55. inline vector_4 operator*( vector_4 const &Multiplicand,
  56.     real const &Multiplier );
  57.  
  58. inline vector_4 operator*( real const &Multiplier,
  59.     vector_4 const &Multiplicand );
  60.     
  61. inline point_4 operator+( point_4 const &Operand1, vector_4 const &Operand2 );
  62. inline point_4 operator-( point_4 const &Operand1, vector_4 const &Operand2 );
  63. inline point_4 operator+( vector_4 const &Operand2, point_4 const &Operand1 );
  64.  
  65. inline vector_4 operator-( vector_4 const &Operand1 );
  66.  
  67. inline vector_4 CrossProduct( vector_4 const &Operand1,
  68.     vector_4 const &Operand2 );
  69.  
  70. inline real DotProduct( vector_4 const &Operand1, vector_4 const &Operand2 );
  71.  
  72. matrix_4x4 operator*( matrix_4x4 const &Multiplicand,
  73.     matrix_4x4 const &Multiplier );
  74.  
  75. vector_4 operator*( matrix_4x4 const &Multiplicand,
  76.     vector_4 const &Multiplier );
  77.  
  78. point_4 operator*( matrix_4x4 const &Multiplicand,
  79.     point_4 const &Multiplier );
  80.  
  81.  
  82.  
  83.  
  84. /*----------------------------------------------------------------------------
  85.  
  86. quadruple.  Base class for homogeneous vectors and points.
  87.  
  88. */
  89.  
  90. class quadruple
  91. {
  92. public:
  93.  
  94.   inline real GetElement( int Row ) const;
  95.  
  96.   inline real GetX( void ) const;
  97.   inline real GetY( void ) const;
  98.   inline real GetZ( void ) const;
  99.   inline real GetW( void ) const;
  100.  
  101.   inline void SetElement( int Row, real Value );
  102.  
  103.   inline void SetX( real Value );
  104.   inline void SetY( real Value );
  105.   inline void SetZ( real Value );
  106.   inline void SetW( real Value );
  107.  
  108.  
  109. protected:
  110.  
  111.   inline quadruple( void );
  112.   inline quadruple( real X, real Y, real Z, real W );
  113.   inline quadruple( quadruple const & );
  114.   inline quadruple &operator=( quadruple const & );
  115.   
  116.   real aElements[4];
  117. };
  118.  
  119.  
  120. /*----------------------------------------------------------------------------
  121.  
  122. point_4.  This class represents a homogeneous 3D point.
  123.  
  124. */
  125.  
  126. class point_4 :
  127.   public quadruple
  128. {
  129. public:
  130.  
  131.   inline point_4( void );
  132.   inline point_4( real X, real Y, real Z );
  133.  
  134.   inline void Homogenize( void );
  135. };
  136.  
  137. /*----------------------------------------------------------------------------
  138.  
  139. vector_4.  This class represents a homogeneous 3D vector.
  140.  
  141. */
  142.  
  143. class vector_4 :
  144.   public quadruple
  145. {
  146. public:
  147.  
  148.   inline vector_4( void );
  149.   inline vector_4( real X, real Y, real Z );
  150.  
  151.   vector_4 &Normalize( void );
  152. };
  153.  
  154.  
  155.  
  156. /*----------------------------------------------------------------------------
  157.  
  158. matrix_4x4.  This class represents row major 4x4 homogeneous matrices.
  159.  
  160. */
  161.  
  162. class matrix_4x4
  163. {
  164. public:
  165.  
  166.   matrix_4x4( void );
  167.  
  168.   matrix_4x4 &ConcatenateXRotation( real Degrees );
  169.   matrix_4x4 &ConcatenateYRotation( real Degrees );
  170.   matrix_4x4 &ConcatenateZRotation( real Degrees );
  171.  
  172.   matrix_4x4 &ConcatenateXTranslation( real Distance );
  173.   matrix_4x4 &ConcatenateYTranslation( real Distance );
  174.   matrix_4x4 &ConcatenateZTranslation( real Distance );
  175.  
  176.   inline real GetElement( int Row, int Column ) const;
  177.   inline matrix_4x4 &SetElement( int Row, int Column, real Value );
  178.  
  179.   
  180. protected:
  181.  
  182.   enum do_not_initialize { DoNotInitialize };
  183.  
  184.   inline matrix_4x4( do_not_initialize );
  185.  
  186.   real aElements[4][4];
  187. };
  188.  
  189. /*----------------------------------------------------------------------------
  190.  
  191. view transform.
  192.  
  193. */
  194.  
  195. class view_transform :
  196.   public matrix_4x4
  197. {
  198. public:
  199.  
  200.   view_transform( point_4 const &Viewpoint, vector_4 const &ViewDirection,
  201.     vector_4 const &Up );
  202. };
  203.  
  204.  
  205. /*----------------------------------------------------------------------------
  206.  
  207. inline function definitions.
  208.  
  209. */
  210.  
  211.  
  212. inline vector_4 operator-( point_4 const &Operand1, point_4 const &Operand2 )
  213. {
  214.   return vector_4(Operand1.GetX() - Operand2.GetX(),
  215.           Operand1.GetY() - Operand2.GetY(),
  216.           Operand1.GetZ() - Operand2.GetZ());
  217. }
  218.  
  219. inline vector_4 operator+( vector_4 const &Operand1,
  220.     vector_4 const &Operand2 )
  221. {
  222.   return vector_4(Operand1.GetX() + Operand2.GetX(),
  223.           Operand1.GetY() + Operand2.GetY(),
  224.           Operand1.GetZ() + Operand2.GetZ());
  225. }
  226.  
  227. inline vector_4 operator-( vector_4 const &Operand1,
  228.     vector_4 const &Operand2 )
  229. {
  230.   return vector_4(Operand1.GetX() - Operand2.GetX(),
  231.           Operand1.GetY() - Operand2.GetY(),
  232.           Operand1.GetZ() - Operand2.GetZ());
  233. }
  234.  
  235. inline vector_4 operator-( vector_4 const &Operand1 )
  236. {
  237.   return vector_4(-Operand1.GetX(),-Operand1.GetY(),-Operand1.GetZ());
  238. }
  239.  
  240. inline vector_4 operator*( vector_4 const &Multiplicand,
  241.     real const &Multiplier )
  242. {
  243.   return vector_4(Multiplicand.GetX() * Multiplier,
  244.           Multiplicand.GetY() * Multiplier,
  245.           Multiplicand.GetZ() * Multiplier);
  246. }
  247.  
  248. inline vector_4 operator*( real const &Multiplier,
  249.     vector_4 const &Multiplicand ) 
  250. {
  251.   return vector_4(Multiplicand.GetX() * Multiplier,
  252.           Multiplicand.GetY() * Multiplier,
  253.           Multiplicand.GetZ() * Multiplier);
  254. }
  255.     
  256. inline point_4 operator+( point_4 const &Operand1, vector_4 const &Operand2 )
  257. {
  258.   return point_4(Operand1.GetX() + Operand2.GetX(),
  259.           Operand1.GetY() + Operand2.GetY(),
  260.           Operand1.GetZ() + Operand2.GetZ());
  261. }
  262.  
  263. inline point_4 operator-( point_4 const &Operand1, vector_4 const &Operand2 )
  264. {
  265.   return point_4(Operand1.GetX() - Operand2.GetX(),
  266.           Operand1.GetY() - Operand2.GetY(),
  267.           Operand1.GetZ() - Operand2.GetZ());
  268. }
  269.  
  270. inline point_4 operator+( vector_4 const &Operand1, point_4 const &Operand2 )
  271. {
  272.   return Operand2 + Operand1;
  273. }
  274.  
  275. inline vector_4 CrossProduct( vector_4 const &Operand1,
  276.     vector_4 const &Operand2 )
  277. {
  278.   real X = Operand1.GetY() * Operand2.GetZ() -
  279.         Operand1.GetZ() * Operand2.GetY();
  280.   real Y = Operand1.GetZ() * Operand2.GetX() -
  281.         Operand1.GetX() * Operand2.GetZ();
  282.   real Z = Operand1.GetX() * Operand2.GetY() -
  283.         Operand1.GetY() * Operand2.GetX();
  284.  
  285.   return vector_4(X,Y,Z);
  286. }
  287.  
  288. inline real DotProduct( vector_4 const &Operand1, vector_4 const &Operand2 )
  289. {
  290.   return Operand1.GetX() * Operand2.GetX() +
  291.       Operand1.GetY() * Operand2.GetY() +
  292.       Operand1.GetZ() * Operand2.GetZ();
  293. }
  294.  
  295.  
  296. inline real quadruple::GetElement( int Row ) const
  297. {
  298.   return aElements[Row];
  299. }
  300.  
  301. inline real quadruple::GetX( void ) const
  302. {
  303.   return aElements[0];
  304. }
  305.  
  306. inline real quadruple::GetY( void ) const
  307. {
  308.   return aElements[1];
  309. }
  310.  
  311. inline real quadruple::GetZ( void ) const
  312. {
  313.   return aElements[2];
  314. }
  315.  
  316. inline real quadruple::GetW( void ) const
  317. {
  318.   return aElements[3];
  319. }
  320.  
  321. inline void quadruple::SetElement( int Row, real Value )
  322. {
  323.   aElements[Row] = Value;
  324. }
  325.  
  326. inline void quadruple::SetX( real Value )
  327. {
  328.   aElements[0] = Value;
  329. }
  330.  
  331. inline void quadruple::SetY( real Value )
  332. {
  333.   aElements[1] = Value;
  334. }
  335.  
  336. inline void quadruple::SetZ( real Value )
  337. {
  338.   aElements[2] = Value;
  339. }
  340.  
  341. inline void quadruple::SetW( real Value )
  342. {
  343.   aElements[3] = Value;
  344. }
  345.  
  346. inline void point_4::Homogenize( void )
  347. {
  348.   aElements[0] = aElements[0] / aElements[3];
  349.   aElements[1] = aElements[1] / aElements[3];
  350.   aElements[2] = aElements[2] / aElements[3];
  351. }
  352.  
  353. inline quadruple::quadruple( void )
  354. {
  355.   aElements[0] = aElements[1] = aElements[2] = aElements[3] = 0;
  356. }
  357.  
  358. inline quadruple::quadruple( real X, real Y, real Z, real W )
  359. {
  360.   aElements[0] = X;
  361.   aElements[1] = Y;
  362.   aElements[2] = Z;
  363.   aElements[3] = W;
  364. }
  365.  
  366. inline quadruple::quadruple( quadruple const &Source )
  367. {
  368.   aElements[0] = Source.aElements[0];
  369.   aElements[1] = Source.aElements[1];
  370.   aElements[2] = Source.aElements[2];
  371.   aElements[3] = Source.aElements[3];
  372. }
  373.  
  374. inline quadruple &quadruple::operator=( quadruple const &Source )
  375. {
  376.   aElements[0] = Source.aElements[0];
  377.   aElements[1] = Source.aElements[1];
  378.   aElements[2] = Source.aElements[2];
  379.   aElements[3] = Source.aElements[3];
  380.  
  381.   return *this;
  382. }
  383.  
  384. inline point_4::point_4( void ) :
  385.   quadruple(0,0,0,1)
  386. {
  387.  
  388. }
  389.  
  390. inline point_4::point_4( real X, real Y, real Z ) :
  391.   quadruple(X,Y,Z,1)
  392. {
  393. #if 0
  394.   char aBuffer[100];
  395.   sprintf(aBuffer,"X: %f Y: %f Z: %f",X,Y,Z);
  396.   MessageBox(0,aBuffer,"foobar",MB_OK);
  397.   sprintf(aBuffer,"X: %f Y: %f Z: %f W:%f",aElements[0],aElements[1],
  398.     aElements[2],aElements[3]);
  399.   MessageBox(0,aBuffer,"foobar",MB_OK);
  400. #endif
  401. }
  402.  
  403. inline vector_4::vector_4( void ) :
  404.   quadruple(0,0,0,0)
  405. {
  406.  
  407. }
  408.  
  409. inline vector_4::vector_4( real X, real Y, real Z ) :
  410.   quadruple(X,Y,Z,0)
  411. {
  412.  
  413. }
  414.  
  415. inline real matrix_4x4::GetElement( int Row, int Column ) const
  416. {
  417.   return aElements[Row][Column];
  418. }
  419.  
  420. inline matrix_4x4 &matrix_4x4::SetElement( int Row, int Column, real Value )
  421. {
  422.   aElements[Row][Column] = Value;
  423.  
  424.   return *this;
  425. }
  426.  
  427. inline matrix_4x4::matrix_4x4( do_not_initialize )
  428. {
  429.  
  430. }
  431.  
  432. #endif
  433.